<?php
/**
 * (logic)
 */
namespace App\Logic\File\Operation;

use App\Models\Product\ProductSku;
use App\Models\Operation\AdsError;
use App\Models\Operation\AdsBC;
class ParseBrandsCampaign
{
    /**
     * 解析AdsBC
     * @param $writeBackId
     * @param $data
     * @param $date
     * @param $countryId
     * @return int
     */
    public static function exeParse($writeBackId,$data,$date,$countryId){
        $items = self::_parseFor($writeBackId,$data,$date,$countryId);
        foreach ($items as $item){
            AdsBC::query()->updateOrCreate(
                ['report_date' => $item['report_date'],
                    'country_id' => $item['country_id'],
                    'psku_id' => $item['psku_id']],
                [
                    'classify_id' => $item['classify_id'],
                    'impressions' => $item['impressions'],
                    'clicks' => $item['clicks'],
                    'total_units' => $item['total_units'],
                    'spend' => round($item['spend'],3),
                    'total_sales' => round($item['total_sales'],3),
                ]
            );
        }
        return true;
    }

    protected static function _parseFor($writeBackId,$data,$date,$countryId){
        unset($data[0]);
        $items = [];
        $skuArr = [];
        $error = [];
        foreach($data as $k => $v){
            $count = count($v);
            if($count == 23){
                $startDate = isset($v[0]) ? date('Y-m-d',strtotime(trim($v[0]))) : '';
                $endDate = isset($v[1]) ? date('Y-m-d',strtotime(trim($v[1]))) : '';
                if(!$startDate || !$endDate)continue;
                if($startDate != $date || $endDate != $date){
                    break;
                }
                $campaignName = isset($v[4]) ? trim($v[4]) : '';
                $impressions = isset($v[5]) ? trim($v[5]) : 0;
                $clicks = isset($v[6]) ? trim($v[6]) : 0;
                $spend = isset($v[9]) ? trim($v[9]) : 0;
                $totalSales = isset($v[12]) ? trim($v[12]) : 0;
                $totalUnits = isset($v[14]) ? trim($v[14]) : 0;
            }else{
                $_date = isset($v[0]) ? date('Y-m-d',strtotime(trim($v[0]))) : '';
                if(!$_date)continue;
                if($_date != $date){
                    break;
                }
                $campaignName = isset($v[3]) ? trim($v[3]) : '';
                $impressions = isset($v[4]) ? trim($v[4]) : 0;
                $clicks = isset($v[5]) ? trim($v[5]) : 0;
                $spend = isset($v[8]) ? trim($v[8]) : 0;
                $totalSales = isset($v[11]) ? trim($v[11]) : 0;
                $totalUnits = isset($v[13]) ? trim($v[13]) : 0;
            }
            if('眼镜 -HD' == $campaignName){
                $campaignName .= '-AM03647';
            }
            $sku = matchSkuByCampaign($campaignName);
            if(!$sku){
                $error[$k]['report_date'] = $date;
                $error[$k]['country_id'] = $countryId;
                $error[$k]['type'] = 11;
                $error[$k]['back_id'] = $writeBackId;
                $error[$k]['error'] = $campaignName;
                continue;
            }
            if(!array_key_exists($sku,$skuArr)){
                $_ps = ProductSku::getModelByCode($sku);
                $skuArr[$sku] = $_ps;
            }
            $ps = $skuArr[$sku];
            if(!$ps){
                $error[$k]['report_date'] = $date;
                $error[$k]['country_id'] = $countryId;
                $error[$k]['type'] = 11;
                $error[$k]['back_id'] = $writeBackId;
                $error[$k]['error'] = $campaignName.'-SKU不存在';
                continue;
            }
            $_spend = replaceAmountChar($spend);
            $_totalSales = replaceAmountChar($totalSales);
            if(array_key_exists($ps->id,$items)){
                $items[$ps->id]['impressions'] += $impressions;
                $items[$ps->id]['clicks'] += $clicks;
                $items[$ps->id]['spend'] += $_spend;
                $items[$ps->id]['total_sales'] += $_totalSales;
                $items[$ps->id]['total_units'] += $totalUnits;
            }else{
                $items[$ps->id] = [
                    'report_date' => $date,
                    'country_id' => $countryId,
                    'psku_id' => $ps->id,
                    'classify_id' => $ps->classify_id,
                    'impressions' => $impressions,
                    'clicks' => $clicks,
                    'spend' => $_spend,
                    'total_sales' => $_totalSales,
                    'total_units' => $totalUnits,
                ];
            }
        }
        if(empty($error)){
            return $items;
        }else{
            AdsError::errorRecord($error);
            return [];
        }
    }
}
